package com.ruoyi.aitymyy.api;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.AitFlow;
import com.ruoyi.aitcommon.anotation.ApiLog;
import com.ruoyi.aitcommon.utils.JSONResult;
import com.ruoyi.aitcommon.utils.MapUtils;
import com.ruoyi.aitymyy.service.YmyySyncSV;

/**
 * 各类同步流程的API接口，可用于手工触发
 * 后期可整合到一个接口中
 * @author Administrator
 */
@RestController
@RequestMapping("/ait/ymyy/flow")
public class YmyySyncAPI {
	@Autowired
	YmyySyncSV dsService;
	

	@AitFlow("手工数据同步API")
	@ApiLog("手工数据同步接口")
	@GetMapping("/handFlow")
	public JSONObject handFlow(String itemstr,Double days) {
		List<JSONObject> retlist=new ArrayList<JSONObject>();
		try {
			if(StringUtils.isBlank(itemstr))
				return JSONResult.error("传入item为空");
			if(days<=0)
				return JSONResult.error("传入天数必须大于0");
			
			String[] items = StringUtils.split(itemstr, ',');
			for (String item : items) {
				if(item.equals("dept"))
					retlist.add(dsService.deptSync(days));
				else if(item.equals("psn"))
					retlist.add(dsService.psnSync(days));
				else if(item.equals("sale"))
					retlist.add(dsService.saleSync(days)) ;
				else if(item.equals("shipment"))
					retlist.add(dsService.shipmentSync(days));
				else if(item.equals("invoice"))
					retlist.add(dsService.invoiceSync(days));
				else if(item.equals("payment"))
					retlist.add(dsService.paymentSync(days));
				else if(item.equals("lxr"))
					retlist.add(dsService.lxrSync());
				else if(item.equals("gys"))
					retlist.add(dsService.gysSync());
				else if(item.equals("gysaddr"))
					retlist.add(dsService.gysaddrSync());
			}
			return JSONResult.success(retlist);
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage(),retlist);
		}
		
	}
	
	@AitFlow("部门数据同步API")
	@ApiLog("部门数据同步接口")
	@PostMapping("/deptFlow")
	public JSONObject deptFlow(@RequestBody String jsonstr) {
		try {
			Object days=MapUtils.getPathObject("data.days",jsonstr);
			if(days==null) days=1;
			return dsService.deptSync(Double.parseDouble(days.toString()));
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage());
		}
	}
	
	@AitFlow("人员数据同步API")
	@ApiLog("人员数据同步接口")
	@PostMapping("/psnFlow")
	public JSONObject psnFlow(@RequestBody String jsonstr) {
		try {
			Object days=MapUtils.getPathObject("data.days",jsonstr);
			if(days==null) days=1;
			return dsService.psnSync(Double.parseDouble(days.toString()));
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage());
		}
	}
	
	@AitFlow("订单数据同步API")
	@ApiLog("订单数据同步接口")
	@PostMapping("/orderFlow")
	public JSONObject orderFlow(@RequestBody String jsonstr) {
		try {
			Object days=MapUtils.getPathObject("data.days",jsonstr);
			if(days==null) days=1;
			return dsService.saleSync(Double.parseDouble(days.toString()));
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage());
		}
	}
	
	@AitFlow("订单发货数据同步API")
	@ApiLog("订单发货数据同步接口")
	@PostMapping("/shipmentFlow")
	public JSONObject shipmentFlow(@RequestBody String jsonstr) {
		try {
			Object days=MapUtils.getPathObject("data.days",jsonstr);
			if(days==null) days=1;
			return dsService.shipmentSync(Double.parseDouble(days.toString()));
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage());
		}
	}
	
	@AitFlow("订单开票数据同步API")
	@ApiLog("订单开票数据同步接口")
	@PostMapping("/invoiceFlow")
	public JSONObject invoiceFlow(@RequestBody String jsonstr) {
		try {
			Object days=MapUtils.getPathObject("data.days",jsonstr);
			if(days==null) days=1;
			return dsService.invoiceSync(Double.parseDouble(days.toString()));
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage());
		}
	}
	
	@AitFlow("订单回款数据同步API")
	@ApiLog("订单回款数据同步接口")
	@PostMapping("/paymentFlow")
	public JSONObject paymentFlow(@RequestBody String jsonstr) {
		try {
			Object days=MapUtils.getPathObject("data.days",jsonstr);
			if(days==null) days=1;
			return dsService.paymentSync(Double.parseDouble(days.toString()));
		}catch(Exception ex) {
			return JSONResult.error(ex.getMessage());
		}
	}
}
